package com.linkgie.galaxyframework.binaryserialize.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.linkgie.galaxyframework.binaryserialize.BinarySliceSpecification;
import com.linkgie.galaxyframework.binaryserialize.FieldDataOptions;
import com.linkgie.galaxyframework.binaryserialize.FieldSpecification;

public abstract class AbstractFieldEncoder implements FieldEncoder {

	protected final BinarySliceSpecification sliceSpec;

	protected final  FieldSpecification fieldSpec;

	protected final  FieldDataOptions fieldOptions;

	protected final  Method reader;

	public AbstractFieldEncoder(BinarySliceSpecification sliceSpec, FieldSpecification fieldSpec, Method reader) {
		this.sliceSpec = sliceSpec;
		this.fieldSpec = fieldSpec;
		this.fieldOptions = new FieldDataOptions(fieldSpec);
		reader.setAccessible(true);
		this.reader = reader;
	}

	@Override
	public BinarySliceSpecification getSliceSpecification() {
		return sliceSpec;
	}

	@Override
	public FieldSpecification getFieldSpecification() {
		return fieldSpec;
	}

	@Override
	public Method getReader() {
		return reader;
	}


	protected Object readValue(Object dataContract) {
		try {
			return reader.invoke(dataContract);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			throw new IllegalStateException(e.getMessage(), e);
		}
	}

//	protected Object[] readArrayValue(Object dataContract) {
//		return (Object[]) readValue(dataContract);
//	}
}
